package com.yijinsuo.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yijinsuo.R;
import com.yijinsuo.base.BaseActivity;
import com.yijinsuo.http.HttpCallBack;
import com.yijinsuo.http.HttpUtils;
import com.yijinsuo.mian.Config;
import com.yijinsuo.tool.MyCountTimer;
import com.yijinsuo.tool.Tool;
import com.yijinsuo.tool.VibratorUtil;
import com.yijinsuo.util.DES;
import com.yijinsuo.view.ClearEditText;
import com.yijinsuo.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 交易密码
 */
public class JiaoYiPasswordActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_Verification_code)
    ClearEditText etVerificationCode;
    @Bind(R.id.et_card_numbers)
    ClearEditText etCardNumbers;
    @Bind(R.id.et_jiaoyi_password)
    ClearEditText etJiaoyiPassword;
    @Bind(R.id.et_confirm_jiaoyi_password)
    ClearEditText etConfirmJiaoyiPassword;
    @Bind(R.id.tv_Verification_Code)
    TextView tvVerificationCode;
    @Bind(R.id.tv_phone)
    TextView tvPhone;

    //
    private MyCountTimer myCountTimer;
    private String userAccount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_jiao_yi_password);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        userAccount = (String) mpreferencesUtils.get(JiaoYiPasswordActivity.this, "userAccount", "");
        titlebar.setTitleBar(R.drawable.back_selector, "设置交易密码", 0);
        tvPhone.setText(Tool.savePhonehidden(userAccount));
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }


    @OnClick({R.id.titlebar_iv_back, R.id.tv_Verification_Code, R.id.tv_password_renzheng})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;
            case R.id.tv_Verification_Code:   //验证码

                //记住账号
                if (null != userAccount) {
                    myCountTimer = new MyCountTimer(60000, 1000, tvVerificationCode, R.string.txt_getMsgCode_validate);
                    myCountTimer.start();

                    //发送验证码
                    sendMessageCode();

                } else {
//                    Toast.makeText(JiaoYiPasswordActivity.this, "您还未登录", Toast.LENGTH_SHORT).show();
                }


                break;


            case R.id.tv_password_renzheng:   //认证
                long[] pattern = {100, 400, 100, 400};
                String VerificationCode = etVerificationCode.getText().toString().trim();
                String CardNumbers = etCardNumbers.getText().toString().trim();
                String JiaoyiPassword = etJiaoyiPassword.getText().toString().trim();
                String ConfirmJiaoyiPassword = etConfirmJiaoyiPassword.getText().toString().trim();

                if (TextUtils.isEmpty(VerificationCode)) {
                    etVerificationCode.setShakeAnimation();
                    VibratorUtil.Vibrate(JiaoYiPasswordActivity.this, pattern, false);
                    Toast.makeText(JiaoYiPasswordActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();

                    return;
                }
//                else if (TextUtils.isEmpty(CardNumbers)) {
//                    etCardNumbers.setShakeAnimation();
//                    VibratorUtil.Vibrate(JiaoYiPasswordActivity.this, pattern, false);
//                    Toast.makeText(JiaoYiPasswordActivity.this, "请输入身份证号", Toast.LENGTH_SHORT).show();
//
//                    return;
//                }
//                else if (!Tool.validatePersonalId(CardNumbers)) {
//                    etCardNumbers.setShakeAnimation();
//                    VibratorUtil.Vibrate(JiaoYiPasswordActivity.this, pattern, false);
//                    Toast.makeText(JiaoYiPasswordActivity.this, "身份证格式不正确", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                else if (TextUtils.isEmpty(JiaoyiPassword)) {
                    etJiaoyiPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(JiaoYiPasswordActivity.this, pattern, false);
                    Toast.makeText(JiaoYiPasswordActivity.this, "请输入交易密码", Toast.LENGTH_SHORT).show();

                    return;
                } else if (TextUtils.isEmpty(ConfirmJiaoyiPassword)) {
                    etConfirmJiaoyiPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(JiaoYiPasswordActivity.this, pattern, false);
                    Toast.makeText(JiaoYiPasswordActivity.this, "请输入确定交易密码", Toast.LENGTH_SHORT).show();

                    return;
                } else {

                    sendJiaoYipassword(userAccount, VerificationCode, CardNumbers, JiaoyiPassword, ConfirmJiaoyiPassword);

                }


                break;

            default:
        }
    }


    /**
     * 交易密码短信
     */

    private void sendMessageCode() {

        if (null != userAccount) {
            HttpParams params = new HttpParams();
            params.put("userAccount", userAccount);
            HttpUtils.post(Config.SNDSMSCODE, params, new HttpCallBack(this, "加载中...") {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {

                    Toast.makeText(JiaoYiPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(String code, String message) {

                    Toast.makeText(JiaoYiPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            });

        } else {

            Toast.makeText(JiaoYiPasswordActivity.this, "您还未登录", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 设置交易密码
     *
     * @param userAccount
     * @param verificationCode
     * @param cardNumbers
     * @param jiaoyiPassword
     * @param confirmJiaoyiPassword
     */

    private void sendJiaoYipassword(String userAccount, String verificationCode, String cardNumbers, String jiaoyiPassword, String confirmJiaoyiPassword) {
        try {
            HttpParams params = new HttpParams();
            params.put("userAccount", userAccount);
            params.put("userPayPassword", DES.encryptDES(jiaoyiPassword,Config.DES_KEY));
            params.put("userPayPassword2", DES.encryptDES(confirmJiaoyiPassword,Config.DES_KEY));
            params.put("IDCard", cardNumbers);
            params.put("phoneCode", DES.encryptDES(verificationCode, Config.DES_KEY));

            HttpUtils.post(Config.USERCENTER, params, new HttpCallBack(this, "加载中...") {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {

                    Toast.makeText(JiaoYiPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                    finish();
                }

                @Override
                public void onError(String code, String message) {

                    Toast.makeText(JiaoYiPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
